package com.tanhua.server.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.server.service.TestSoulService;
import com.tanhua.server.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("testSoul")
public class TestSoulController {

    @Autowired
    private TestSoulService testSoulService;


    /**
     * 测灵魂-问卷列表  传入token
     *
     * @return
     */
    @GetMapping
    public List<QuestionaireVo> queryQuestionaireList() {

        try {
            //成功
            List<QuestionaireVo> questionaireVoList = testSoulService.queryQuestionaireList();

            //不为空
            if (questionaireVoList != null) {
                //返回questionaireVoList集合
                return questionaireVoList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //失败
        return null;

    }


    /**
     * 测灵魂-提交问卷
     *
     * @return
     */
    @PostMapping
    public ResponseEntity<String> saveQuestionList(@RequestBody AnswersList answers) {


        try {

            List<Answers> answersList = answers.getAnswers();
            String reportId = testSoulService.saveQuestionList(answersList);
            System.out.println(reportId);
            if (reportId != null) {
                return ResponseEntity.ok(reportId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //失败
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }


    /**
     * 测灵魂-查看结果
     *
     * @param id 报告id
     * @return
     */
    @GetMapping("report/{id}")
    public TestSoulResultVo queryQuestionResult(@PathVariable String id) {
        try {
            //实现
            TestSoulResultVo testSoulResultVo = this.testSoulService.queryQuestionResult(id);

            if (testSoulResultVo != null) {

                return testSoulResultVo;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //失败
        return null;

    }
}
